package com.ithao.controller;

import com.ithao.common.GlobalConst;
import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.entity.Result;
import com.ithao.pojo.Catalog;
import com.ithao.pojo.User;
import com.ithao.service.CatalogService;
import com.ithao.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @description ：学科目录控制器
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    /**
     * 添加学科目录
     */
    @RequestMapping("/add")
    public Result addCatalog(@RequestBody Catalog catalog, HttpServletRequest request) {
        try {
            // 初始化用户ID，状态和创建时间
            User user = (User) request.getSession().getAttribute(GlobalConst.SESSION_KEY_USER);
            if (user != null) {
                catalog.setUserId(user.getId());
            } else {
                catalog.setUserId(1); //默认admin
            }
            catalog.setStatus(0);
            catalog.setCreateDate(DateUtils.parseDate2String(new Date()));
            catalogService.addCatalog(catalog);
            return new Result(true, "添加学科目录成功");
        } catch (RuntimeException e) {
            log.error("addCatalog", e);
            return new Result(false, e.getMessage());
        }
    }

    /**
     * 分页获取学科目录列表
     */
    @RequestMapping("findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean == null) {
            queryPageBean = new QueryPageBean();
            queryPageBean.setCurrentPage(1);
            queryPageBean.setPageSize(10);
        }
        log.info("findListByPage questionList queryPageBean:{}", queryPageBean);
        PageResult pageResult = catalogService.findListByPage(queryPageBean);
        return new Result(true, "获取学科目录列表成功", pageResult);
    }

    /**
     * 删除学科目录
     */
    @RequestMapping("delete")
    public Result deleteCatalog(Integer catalogId) {
        try {
            log.info("deleteCatalog id:", catalogId);
            catalogService.deleteCatalog(catalogId);
            return new Result(true, "删除学科目录成功");
        } catch (RuntimeException e) {
            log.error("deleteCatalog", e);
            return new Result(false, e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Catalog catalog) {
        log.info("[更新目录] catalog:{}",catalog);
        catalogService.update(catalog);
        return new Result(true,"更新成功");
    }
    /**
     * 目录的启用禁用
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestBody Map map) {
        if (map == null || map.size() == 0) {
            throw new RuntimeException("参数为空");
        }
        Integer catalogId = (Integer) map.get("catalogId");
        Integer status = (Integer) map.get("status");
        log.info("[目录的禁用启用 ] catalogId:{}   status:{}", catalogId, status);
        if (status == 0) {
            catalogService.updateStatus(catalogId, 1);
            return new Result(true, "目录启动成功");
        } else  {
            catalogService.updateStatus(catalogId, 0);
            return new Result(true, "目录禁用成功");
        }
    }

}
